package org.zcl.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.zcl.entity.SeckillProducts;
import org.zcl.model.ApiResponse;
import org.zcl.model.stock.StockRequest;
import org.zcl.service.StockService;

/**
 * .
 *
 * @author zhuchenglin
 * @since 2024/6/18 16:22
 */
@RestController
public class StockController {

    @Autowired
    private StockService stockService;

    @RequestMapping(value = "stock", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse stock(@RequestBody StockRequest stockRequest) {
        // 商品ID
        long commodityId = stockRequest.getProductId();
        // 库存ID
        String redisKey = "redis_key:stock:" + commodityId;
        Boolean data = stockService.stock(stockRequest, redisKey, 60 * 60, stockRequest.getNum());
        return ApiResponse.builder().code("0").msg("success").data(data).build();
    }

    @RequestMapping(value = "initStock", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse initStock(@RequestParam Long commodityId) {
        String redisKey = "redis_key:stock:" + commodityId;
        SeckillProducts data = stockService.init(commodityId, redisKey, 60 * 60);
        return ApiResponse.builder().code("0").msg("success").data(data).build();
    }

    @RequestMapping(value = "getStock", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object getStock() {
        // 商品ID
        long commodityId = 1;
        // 库存ID
        String redisKey = "redis_key:stock:" + commodityId;

        return stockService.getStock(redisKey);
    }

    @RequestMapping(value = "addStock", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object addStock() {
        // 商品ID
        long commodityId = 2;
        // 库存ID
        String redisKey = "redis_key:stock:" + commodityId;

        return stockService.addStock(redisKey, 2);
    }
}
